<?php
App::uses('AppController', 'Controller');
App::import('Vendor', 'Uploader.Uploader');
/**
 * Banners Controller
 *
 * @property Banner $Banner
 */
class BannersController extends AppController {

    public function beforeFilter() {
        $this->Uploader = new Uploader(array(
            'ajaxField' => 'qqfile'
        ));

    }
/**
 * admin_index method
 *
 * @return void
 */
    public function admin_index() {
        $this->Banner->recursive = 0;
        $this->set('banners', $this->paginate());
    }

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function admin_view($id = null) {
        if (!$this->Banner->exists($id)) {
            throw new NotFoundException(__('Invalid banner'));
        }
        $options = array('conditions' => array('Banner.' . $this->Banner->primaryKey => $id));
        $this->set('banner', $this->Banner->find('first', $options));
    }

/**
 * admin_add method
 *
 * @return void
 */
    public function admin_add() {
        $this->Uploader->uploadDir = '/img/banners/';
        if ($this->request->is('post')) {
            $datarequest = $this->request->data;
            //debug($datarequest);die;
            if (!empty($this->request->data)) {
            if ($data = $this->Uploader->upload($datarequest['Banner']['link'], array('name' => 'uploaderFilename', 'overwrite' => true))) {
                $path = $this->Uploader->resize(array('width' => 890, 'height' => 407, 'expand' => true, 'aspect' => false));
                // $dataimg['name'] = $data['custom_name'].'-resized-' . '890' . 'x' . '407';
                $dataimg['name'] = $datarequest['Banner']['name'];
                $dataimg['link'] = $path;
                $this->Uploader->delete($data['path']);

            $this->Banner->create();
            if ($this->Banner->save($dataimg)) {
                $this->Session->setFlash(__('The banner has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The banner could not be saved. Please, try again.'));
            }
            }
        }

        }


    }

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function admin_edit($id = null) {
        if (!$this->Banner->exists($id)) {
            throw new NotFoundException(__('Invalid banner'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Banner->save($this->request->data)) {
                $this->Session->setFlash(__('The banner has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The banner could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Banner.' . $this->Banner->primaryKey => $id));
            $this->request->data = $this->Banner->find('first', $options);
        }
    }

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function admin_delete($id = null) {
        $this->Banner->id = $id;
        $dataold = $this->Banner->findById($id);
        if (!$this->Banner->exists()) {
            throw new NotFoundException(__('Invalid banner'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Banner->delete()) {
            $this->Uploader->delete($dataold['Banner']['link']);
            $this->Session->setFlash(__('Banner deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Banner was not deleted'));
        $this->redirect(array('action' => 'index'));
    }
}
